package com.jp.knowledge.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jp.knowledge.R;
import com.jp.knowledge.logic.TopicLogic;
import com.jp.knowledge.logic.TopicLogicCallBack;
import com.jp.knowledge.model.ReplyListInfo;
import com.jp.knowledge.util.TimeUtil;

import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * Created by jiayen on 16/10/21.
 */

public class TopicDetailAdapter extends  JPBaseAdpter<ReplyListInfo> implements View.OnClickListener {

    private TopicLogic topicLogic;
    private String infoid;
    private String topicId;

    public void setInfoid(String infoid) {
        this.infoid = infoid;
    }

    public void setTopicId(String topicId) {
        this.topicId = topicId;
    }

    @Override
    public void updataAdapter(final List<ReplyListInfo> infos) {
//        if(topicLogic==null){
            topicLogic = new TopicLogic(getContext(), new TopicLogicCallBack() {
                @Override
                public void priseSuccess(int position) {
                    if(infos!=null){
                        infos.get(position).setIsPraise(1);
                        infos.get(position).setPraiseNum( infos.get(position).getPraiseNum()+1);
                        updataAdapter(infos);
                    }
                }
            });
//        }
        super.updataAdapter(infos);
    }
    public TopicDetailAdapter(Context context, final  List<ReplyListInfo> infos) {
        super(context, infos);
        if(infos!=null&&topicLogic==null){
            topicLogic = new TopicLogic(getContext(), new TopicLogicCallBack() {
                @Override
                public void priseSuccess(int position) {
                    if(infos!=null){
                        infos.get(position).setIsPraise(1);
                        updataAdapter(infos);
                    }
                }
            });
        }
    }

    @Override
    protected int getLayoutId(int position) {
        return   R.layout.topic_detail_item;
    }


    @Override
    protected void renderData(JPViewHolder holder, ReplyListInfo item, int position) {

        TextView name =holder.getView (R.id.name);
        TextView prise =holder.getView (R.id.prise);
        ImageView  photo =holder.getView (R.id.photo);
        TextView  content= holder.getView (R.id.content);
        LinearLayout replyLayout= holder.getView (R.id.reply_info_layout);
        TextView replyName= holder.getView (R.id.reply_name);
        TextView time = holder.getView(R.id.time);

       String replyUid =  item.getReplyData().getUid();
        String uid = item.getUid();
        time.setText(TimeUtil.friendly_time(item.getTime()));
        name.setText(item.getNickName());
        prise.setText(item.getPraiseNum()+"");
        prise.setTag(position);
        prise.setOnClickListener(this);
        if(item.getIsPraise()==1){//说明是点赞的
            prise.setOnClickListener(null);
            prise.setCompoundDrawablesWithIntrinsicBounds(getContext().getResources().getDrawable(R.mipmap.toutiaoxiangqingyidianzan),null,null,null);
        }else{//说明是没有点赞的
            prise.setOnClickListener(this);
            prise.setCompoundDrawablesWithIntrinsicBounds(getContext().getResources().getDrawable(R.mipmap.toutiaoxiangqingzan),null,null,null);
        }



        Glide.with(getContext()).load(item.getPortrait()).bitmapTransform(new CropCircleTransformation(getContext()))
                .fitCenter().placeholder(R.mipmap.morentouxiang)
                .override(30,30).into(photo);

        content.setText(item.getContent());

        if(replyUid!=null&&!replyUid.equals(uid)){
            replyLayout.setVisibility(View.VISIBLE);
            replyName.setText(item.getReplyData().getUserName());
        }else{
            replyLayout.setVisibility(View.GONE);
        }

    }

    @Override
    public void onClick(View view) {
        int position = (int) view.getTag();
        ReplyListInfo info = mInfos.get(position);
        topicLogic.topicPrise(infoid, topicId,info.getCommentId(), position);
    }
}
